package com.bing.common.component;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.bing.common.exception.BusinessException;

import java.util.Collection;

/**
 * @Description 数据权限
 * @Author Bing
 * @Date 2025/8/6 17:13
 */
public class InQueryWrapper<T> {

    private SFunction<T, ?> columns[];

    private Collection<?> columnsVal[];

    private LambdaQueryWrapper<T> queryWrapper;

    private InQueryWrapper(LambdaQueryWrapper<T> queryWrapper){
        this.queryWrapper = queryWrapper;
    }

    public InQueryWrapper<T> columns(SFunction<T, ?>... columns) {
        this.columns = columns;
        return this;
    }

    public InQueryWrapper<T> columnsVal(Collection<?> ...columnsVal) {
        this.columnsVal = columnsVal;
        return this;
    }

    public LambdaQueryWrapper<T> build() {
        check();
        for(int i = 0 ; i < columns.length; i++){
            queryWrapper.in(columns[i], columnsVal[i]);
        }
        return queryWrapper;
    }

    /**
     * 构建
     * @param queryWrapper
     * @return
     * @param <T>
     */
    public static <T> InQueryWrapper<T> of(LambdaQueryWrapper<T> queryWrapper) {
        return new InQueryWrapper<>(queryWrapper);
    }

    /**
     * 校验
     */
    private void check(){
        if(ObjectUtil.isNull(columns)){
            BusinessException.throwBusException("columns is not null");
        }
        if(ObjectUtil.isNull(columnsVal)){
            BusinessException.throwBusException("columnsVal is not null");
        }
        if(columns.length != columnsVal.length){
            BusinessException.throwBusException("columns and columnsVal length is not equal");
        }
    }
}